<?php

/**
 * do_action()
 *
 * @param string $sAction
 * @param mixed $mArgs
 * @return
 */
function do_action($sAction = "", $mArgs = null)
{
    return call_user_func_array($sAction, $mArgs);
}

/**
 * zeus_charset()
 *
 * @return
 */
function zeus_charset()
{
    return ZEUS_CHARSET;
}

/**
 * zeus_style()
 *
 * @param string $sFileName
 * @return
 */
function zeus_style($sFileName = "",$sMedia="screen")
{
    $style = "\n<LINK href='"
     . "/includes/css/" . $sFileName . ".css?version=" . ZEUS_VERSION
     . "' media='".$sMedia
	 ."' type='text/css' rel='stylesheet' />";
    return $style;
}

/**
 * zeus_page_title()
 *
 * @return
 */
function zeus_page_title()
{
    $page = substr($_SERVER['SCRIPT_NAME'], 0, -4);
    $_SESSION['page'] = $page;
    // Determine what page to display
    switch ($page) {
        case ZEUS_DASH . "edit_user":
            $pageTitle = _e('Manage Users');
            break;
        case ZEUS_ROOT . "index":
            break;
            $pageTitle = _e('Site Login Page');
        case ZEUS_DASH . "index":
            $pageTitle = _e('Dash Board Page');
            break;
        case ZEUS_DASH . "profile":
            if ($_SESSION['is_profile_page']) {
                $pageTitle = _e('Your Profile');
            } else {
                $pageTitle = _e('Edit User');
            }
            break;
        default:
            $pageTitle = _e('SmartVision MIS');
            break;
    }
    return $pageTitle;
}
/**
 * get_privilege()
 *
 * @param string $sUserId
 * @return
 */
function get_privilege($sUserId = "")
{
    if (!empty($sUserId)) {
        $sql = "SELECT privilege FROM " . DB_PREFIX . "privilege WHERE user_id='" . $sUserId . "';";
        $privileges = zeus_sql_query($sql);
    } else {
        $privileges = false;
    }
    return $privileges;
}
/**
 * zeus_site_name()
 *
 * @return
 */
function zeus_site_name()
{
    $name = _e('SmartVision Develop Team');
    return $name;
}
/**
 * logout()
 *
 * @return
 */
function logout()
{
    if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time()-42000, '/');
    }
    session_destroy();
}
/**
 * can_operate()
 *
 * @param string $sAction
 * @return
 */
function can_operate($sAction = '', $sUserId = '')
{
    $privileges = get_privilege($sUserId);
    $user_power = explode(",", $privileges['privilege']);
    $action_id = get_action_id($sAction);
    return in_array($action_id, $user_power);
}
/**
 * get_action_id()
 *
 * @param string $sAction
 * @return
 */
/**
 * get_action_id()
 *
 * @param string $sAction
 * @return
 */
function get_action_id($sAction = '')
{
    $sql = "SELECT page_id FROM " . DB_PREFIX . "modules WHERE page_url='" . $sAction . "'";
    $result = zeus_sql_query($sql, false);
    if (!empty($result)) {
        return $result['page_id'];
    } else {
        return false;
    }
}
/**
 * zeus_do_nothing()
 *
 * @return
 */
function zeus_do_nothing()
{
    // Do nothing
}

/**
 * zeus_js()
 *
 * @param string $sFileName
 * @return
 */
function zeus_js($sFileName = "")
{
    $js = "\n<SCRIPT type=\"text/javascript\" src=\"" . "/includes/js/" . $sFileName . ".js\"></SCRIPT>";
    return $js;
}

/**
 * zeus_redirect()
 *
 * @param string $sUrl
 * @return
 */
function redirect($sUrl = "")
{
    session_write_close();
    header("Location: " . $sUrl);
    exit ();
}
/**
 * getAdminPrivileges()
 *
 * @return
 */
function getAdminPrivileges()
{
    $sql = "SELECT page_id FROM " . DB_PREFIX . "modules";
    $result = zeus_sql_query($sql, false, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $power) {
            $allPower[] = $power['page_id'];
        }
        return implode(",", $allPower);
    } else {
        return false;
    }
}
/**
 * updateAdminPrivileges()
 *
 * @return
 */
function updateAdminPrivileges()
{
    $sql = "UPDATE " . DB_PREFIX . "role SET capability='" . getAdminPrivileges() . "' WHERE role_name='Administrator'";
    if (0 != zeus_sql_exec($sql, false)) {
        return true;
    } else {
        return false;
    }
}
/**
 * getAllPower()
 *
 * @return
 */
function getAllPower()
{
    $sql = "SELECT role_id,role_name FROM " . DB_PREFIX . "role";
    $result = zeus_sql_query($sql, false, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $value) {
            $id[] = $value['role_id'];
            $name[] = _e($value['role_name']);
        }
        return array_combine($id, $name);
    } else {
        return false;
    }
}
/**
 * getAllSex()
 *
 * @return
 */
function getAllSex()
{
    $sql = "SELECT sex_id,sex_name FROM " . DB_PREFIX . "sex";
    $result = zeus_sql_query($sql, false, 'All');
    if (isset($result) && null != $result) {
        foreach($result AS $value) {
            $id[] = $value['sex_id'];
            $name[] = _e($value['sex_name']);
        }
        return array_combine($id, $name);
    } else {
        return false;
    }
}

function getIP(){
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
           $ip = getenv("HTTP_CLIENT_IP");
       else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
           $ip = getenv("HTTP_X_FORWARDED_FOR");
       else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
           $ip = getenv("REMOTE_ADDR");
       else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
           $ip = $_SERVER['REMOTE_ADDR'];
       else
           $ip = "unknown";
   return($ip);
}

?>